home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / TIMEGETC.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  822b  |  45 lines

  1. /*
  2. **  TIMEGETC.C - waits for a given number of seconds for the user to press
  3. **               a key.  Returns the key pressed, or EOF if time expires
  4. **
  5. **  by Bob Jarvis
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <conio.h>
  11.  
  12. int timed_getch(int n_seconds)
  13. {
  14.       time_t start, now;
  15.  
  16.       start = time(NULL);
  17.       now = start;
  18.  
  19.       while(difftime(now, start) < (double)n_seconds && !kbhit())
  20.       {
  21.             now = time(NULL);
  22.       }
  23.  
  24.       if(kbhit())
  25.             return getch();
  26.       else  return EOF;
  27. }
  28.  
  29. #ifdef TEST
  30.  
  31. void main(void)
  32. {
  33.       int c;
  34.  
  35.       printf("Starting a 5 second delay...\n");
  36.  
  37.       c = timed_getch(5);
  38.  
  39.       if(c == EOF)
  40.             printf("Timer expired\n");
  41.       else  printf("Key was pressed, c = '%c'\n", c);
  42. }
  43.  
  44. #endif /* TEST */
  45.